﻿@page "/authentication/login"
@layout EmptyLayout


@using Bookify.Web.Services.User


<div class="d-flex align-center" style="height:calc(100% - 96px)">
    <MCard Width=510 Height=680 Class="mx-auto my-auto">
        <div style="height:60px;"></div>
        <div class="mt-2 mx-auto text-center" style="width:420px;">
            <MAvatar Size=80>
                <MImage Src="/img/avatar/1.svg">
                </MImage>
            </MAvatar>
            <h2 class="mt-2 mb-12">欢迎使用公寓预定系统! 👋</h2>

            <MTextField TValue="string"
            Label="Email"
            Placeholder="john@example.com"
            Outlined @bind-Value="@email"
            HideDetails="@("auto")"> 

            </MTextField>
            <MTextField TValue="string"
            Class="mt-10"
            Label="Password"
            Type="@(_show ? "text" : "password")"
            Placeholder="john@example.com"
            AppendIcon="@(_show ? "mdi-eye" : "mdi-eye-off")"
            OnAppendClick="() => _show = !_show"
            Outlined @bind-Value="@password"
            HideDetails="@("auto")">

            </MTextField>
            <span class="d-flex justify-end mt-1 text-caption hover-pointer">
                Forgot Password?
            </span>
            <MButton Class="mt-11 rounded-4 text-btn white--text" Rounded OnClick=OnLogin Height=46 Width=340 Color="primary">登录</MButton>

            <div class="my-6">            
                <span class="primary--text hover-pointer">注册新账号</span>
            </div>
           

        </div>
    </MCard>
</div>

@code {
    private bool _show;
    private string email = string.Empty;
    private string password = string.Empty;

    [Inject]
    public NavigationManager Navigation { get; set; } = default!;

    [Inject]
    public UserCaller UserCaller { get; set; } = default!;



    [Parameter]
    public bool HideLogo { get; set; }

    [Parameter]
    public double Width { get; set; } = 410;

    [Parameter]
    public StringNumber? Elevation { get; set; }

    [Parameter]
    public string CreateAccountRoute { get; set; } = $"pages/authentication/register-v1";

    [Parameter]
    public string ForgotPasswordRoute { get; set; } = $"pages/authentication/forgot-password-v1";


    [Parameter]
    public EventCallback<MouseEventArgs> OnLogin { get; set; }

    // 将 OnLogin 事件绑定到 HandleLogin 方法
    protected override void OnInitialized()
    {
        OnLogin = EventCallback.Factory.Create<MouseEventArgs>(this, HandleLogin);
    }
    


    private async Task HandleLogin(MouseEventArgs e)
    {
        // 调用 LoginAsync 方法
        var isLogin = await UserCaller.LoginAsync(email, password);

        if (isLogin)
        {
            
            // 登录成功，跳转到主页或其他页面
            Navigation.NavigateTo("/");
        }
        else
        {
            // 登录失败，提示用户
            Console.WriteLine("登录失败，请检查邮箱和密码");
        }
    }
    
}
